/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package modelo;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import modelo.exceptions.NonexistentEntityException;
import modelo.exceptions.PreexistingEntityException;
import proyecto.modelo.InvIngresodet;
import proyecto.modelo.InvIngresodetPK;

/**
 *
 * @author sistemas
 */
public class InvIngresodetJpaController {

    public InvIngresodetJpaController() {
        emf = Persistence.createEntityManagerFactory("dew-xpPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(InvIngresodet invIngresodet) throws PreexistingEntityException, Exception {
        if (invIngresodet.getInvIngresodetPK() == null) {
            invIngresodet.setInvIngresodetPK(new InvIngresodetPK());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(invIngresodet);
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findInvIngresodet(invIngresodet.getInvIngresodetPK()) != null) {
                throw new PreexistingEntityException("InvIngresodet " + invIngresodet + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(InvIngresodet invIngresodet) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            invIngresodet = em.merge(invIngresodet);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                InvIngresodetPK id = invIngresodet.getInvIngresodetPK();
                if (findInvIngresodet(id) == null) {
                    throw new NonexistentEntityException("The invIngresodet with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(InvIngresodetPK id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            InvIngresodet invIngresodet;
            try {
                invIngresodet = em.getReference(InvIngresodet.class, id);
                invIngresodet.getInvIngresodetPK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The invIngresodet with id " + id + " no longer exists.", enfe);
            }
            em.remove(invIngresodet);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<InvIngresodet> findInvIngresodetEntities() {
        return findInvIngresodetEntities(true, -1, -1);
    }

    public List<InvIngresodet> findInvIngresodetEntities(int maxResults, int firstResult) {
        return findInvIngresodetEntities(false, maxResults, firstResult);
    }

    private List<InvIngresodet> findInvIngresodetEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from InvIngresodet as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public InvIngresodet findInvIngresodet(InvIngresodetPK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(InvIngresodet.class, id);
        } finally {
            em.close();
        }
    }

    public int getInvIngresodetCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from InvIngresodet as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
